﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Entity.Car_Rental;
using Data.Car_Rental;
using Entity;
using Common.Background.Web.Controllers;
using Data;
using Entity.CommonEnums;
namespace Background.Controllers
{
    [ValidateInput(false)]
    public class ProductController : BackgroundBaseController
    {
        IProductRepository iProductRepository = null;
        IProduct_Property_RRepository iProduct_Property_RRepository = null;
        public ProductController()
        {
            iProductRepository = new ProductRepository();
            iProduct_Property_RRepository = new Product_Property_RRepository();
        }
        //
        // GET: /Product/

        public ActionResult Index(int? page)
        {
            VP = new VPredication();
            PP = new PagingParam(page ?? 1, base.PageSize);
            var model = iProductRepository.GetProduct(VP, PP);
            return View(model);
        }

        //
        // GET: /Product/Create

        public ActionResult Details(int? id)
        {
            if (id.HasValue)
                return View(iProductRepository.GetDetailModel().FirstOrDefault(i => i.ProductID == id.Value));
            return View();
        }

        //
        // POST: /Product/Create[添加与编辑]

        [HttpPost]
        public ActionResult Details(FormCollection collection)
        {
            Product entity = new Product();
            TryUpdateModel(entity);
            entity.CreateDate = DateTime.Now;
            entity.UpdateDate = DateTime.Now;
            entity.Status = (int)Status.Normal;
            entity.PropertyInfo = string.Empty;
            entity.Image = collection["image"];
            entity.SmallImage = collection["image"];
            string[] arr = collection.GetValues("selList");
            if (arr != null && arr.Any())
            {
                List<Product_Property_R> list = new List<Product_Property_R>();
                Array.ForEach(arr, i =>
                {
                    list.Add(new Product_Property_R { SortNumber = 0, ProductPropertyID = Convert.ToInt32(i) });
                });
                entity.Product_Property_R_Extend = list;
            }
            if (entity.IsValid)
            {
                if (entity.ProductID == 0)
                    iProductRepository.Insert(entity);
                else
                    iProductRepository.Update(entity);
                return AlertToUrl("Index");
            }
            else
            {
                entity.GetRuleViolations().ToList().ForEach(i =>
                {
                    ModelState.AddModelError(i.PropertyName, i.ErrorMessage);
                });
            }
            return View(iProductRepository.GetDetailModel().FirstOrDefault(i => i.ProductID == entity.ProductID));
        }


        //
        // GET: /Product/Delete/5

        public ActionResult Delete(int id)
        {
            iProductRepository.Delete(new Product { ProductID = id });
            return AlertToUrl("Index");
        }



        public ActionResult GetPropertyList(int? typeID = 1)
        {
            List<Entity.Car_Rental.ProductProperty> productPropertyList = ProductPropertyRepository.Instance.Where(i => i.PropertyTypeID == typeID).ToList();

            return View(productPropertyList);
        }
    }
}
